Commands
The main interface of Git is the command line. As such, it is important for users to be familiar with all commonly used commands and their arguments. The usage of each command is structured in the same way. Each command begins with the keyword, followed by any flags, and finally followed by any additional arguments. Setup and Config Three commands comprise setup and config for Git: git, config,'' and, ''help. These commands allow a user to setup and configure the Git environment. Git https://git-scm.com/docs/git The git command is the base command through which all other commands are accessible. To access and other Git command, they must be preceded by the git keyword. Using this keyword alone also has its uses. With the appropriate flags the Git version number and some help information can be printed Config https://git-scm.com/docs/git-config The config command is used to view and change local or global configuration options. These options are important as they control the current user and signature of branch commits. Help https://git-scm.com/docs/git-help The help command is used to view general or command specific help information directly through the console. Information can be given in a brief overview, a verbose explanation, or through a link to official Git references. Creating Projects When using Git, after configuring the global Git environment, the next step is to create a new Git repository or to clone an existing repository as a local copy. The following commands will allow a user to create a new, local repository given several options. Init https://git-scm.com/docs/git-init The init command allows a user to create a new, empty repository. New repositories can be created from the standard template, a specified custom template, or as a bare repository. Clone https://git-scm.com/docs/git-clone The clone command allows a user to clone an existing local or remote repository inside a new, local repository. When cloning a repository, the targeted branch can be specified to begin editing that branch. Additionally, the origin repository can be named for easy reference for future merges or commits. Snapshotting Snapshotting refers the the set of features concerned with managing tracked files and some aspects of saved states. Using snapshotting features allows a programmer to control how local files are managed by a repository Add https://git-scm.com/docs/git-add The add command updates the Git index with the specified added files. This allows new files to be tracked by Git. Because Git can be configured to ignore certain file types, each new file must be added using this command to be tracked and to be able to be committed to a branch. Status https://git-scm.com/docs/git-status The status command allows a user to view the differences between the index file and the current HEAD commit. This command shows files that would be committed by using the commit command and files that can be committed by first using the add command. Diff https://git-scm.com/docs/git-diff The diff command gives detailed information on the difference between two commits, between a commit and the working tree, or between two local files. This command can be used to see more explicitly what one has changed since the last commit to appropriately label the current commit. Commit https://git-scm.com/docs/git-commit The commit command is perhaps the most important Git command. This command collects all changes stored in the index and records those changes in the repository. In Git, a commit is analogous to a save. After a feature has been fully implemented, one can commit these changes, tag the commit with author information, and append a message to the commit describing the changes made. Only changes to files that have been added to the Git index will be updated when committing. Reset https://git-scm.com/docs/git-reset The reset command is used to revert the current HEAD to a specified state. There are a few types of resets. A soft reset does not change the index file or the working tree and only reverts the HEAD. A hard reset reverts both the index and the working tree and discards and tracked changes since the roll-back point. Several mixed resets are allowed as well which revert only the index or working tree. Rm https://git-scm.com/docs/git-rm The rm command is used to remove files from the working tree and index. This means that Git will no longer track changes to these files. Mv https://git-scm.com/docs/git-mv The mv command is used to move or rename files and directories. Tracked files must be changed this way to preserve the existence of the new files in the index and working tree. Branching Branching is by far the most useful feature included in Git. Branches refer to seperate chains (or branches) of commits to a repository. Branching commands provide a developer with the tools to create, select, inspect, modify, and merge branches in a repository. Branch https://git-scm.com/docs/git-branch The branch command is used in several ways. If given no arguments, it can be used to list all existing branches in a repository. The command can also be used to delete or copy existing branches. Checkout https://git-scm.com/docs/git-checkout The checkout command is used to switch current branches. A user can switch to another existing branch by specifying the branch name or checkout a new branch by using the -b flag and a new branch name. Merge https://git-scm.com/docs/git-merge The merge command allows two branches of a repository to be merged into a single branch. Merges can be made using standard behavior or fast-forward mode. Fast-forward mode can be used when the updated parts to the branch can be automatically detected. Fast-forward merges are treated as if the commits in the merging branch were actually part of the main branch. Mergetool https://git-scm.com/docs/git-mergetool The mergetool is used to resolve merge conflicts when fast-forward merges are not possible. The mergetool will show files with differences that are not able to be resolved and allow the user to pick which file's changes should be kept. The mergetool is an important part of Git because any complex branch merging must be managed through some form of the mergetool. Log https://git-scm.com/docs/git-log The log command is a useful tool which shows the commits logs of a branch. Parameters can be used to specify the revision range or size of log. Stash https://git-scm.com/docs/git-stash The stash command allows a user to revert beck to a clean working directory and store any changes made between that point and the current state. Stashes can be stored in a local directory or in a new branch. Stashes allow a user to keep his working history clean but keep important changes that will be implemented in the future or in separate branch. Tag https://git-scm.com/docs/git-tag The tag command allows a user to manage repository tags. A tag is an additional label given to a repository commit. Tags are commonly used to explicitly specify version numbers or release versions. Using the tag command, a user can create, list, delete, or verify repository tag objects. Worktree https://git-scm.com/docs/git-worktree The worktree command allows users to manage multiple worktrees associated with the same repository. Using the command, one is able to add, list, lock, unlock, move, prune, and remove worktrees. Using multiple worktrees is advised when one must quickly change tasks, such as needing to resolve a major bug while working on a feature. By stashing changes and using a separate worktree, the history of both changes are tracked separately and cleanly. Updating Projects When changes have been made to a local or remote repository, the respective other repository must be updated to reflect these changes. With the below commands, a developer can request remote changes, apply remote changes to a local repository, apply local changes to a remote repository, and modify the structure of a repository to reflect the interrelated nature of sub-components of a project. Fetch https://git-scm.com/docs/git-fetch The fetch command is used to download files and refs from a remote repository. The fetch command will update the tracking of a remote repository on a local repository without making any changes to the local repository. Fetch should be used to update references to a local repository before attempting to merge with or modify remote branches. Pull https://git-scm.com/docs/git-pull The pull command is used to pull the changes from a remote branch and apply them to the current local branch. Using pull, internally, calls fetch to update the local repository with remote changes and merge to apply the changes to the local branch. Push https://git-scm.com/docs/git-push The push command is the same as the pull command but merging in the opposite direction. Using push, one updates a specified remote branch with the currently active local branch. Push is the command one should use when integrating new local changes into the remote repository. Remote https://git-scm.com/docs/git-remote The remote command is used to manage the connection to remote repositories and branches tracked by the local repository. Using additional arguments to remote, a programmer is able to add or remove remotes, rename remotes, modify tracked branches, and modify remote URLs. Submodule https://git-scm.com/docs/git-submodule The submodule command is used to create, update, or inspect repository submodules. Project submodules can used to manages multiple related projects that may share resources. Using submodules instead of seperate repositories allow the related modules to be stored jointly, share resource files, and accessed in a more useful way. Inspection & Comparison A key feature of Git is the ability to view detailed information about each recorded change, or commit, on a repository. The inspection and comparison commands allow a developer to view the details of commits, apply simple labels to objects, and compare repositories before and after commits. Showhttps://git-scm.com/docs/git-show The show command is used to show information about a number of different types of tracked objects. For commit objects, show ''will display the log message and differences between pre and post commit. For tag objects, ''show will display the tag message and referenced objects. For tree objects, the names will be displayed. For other objects, show will display the plain text contents. Log https://git-scm.com/docs/git-log The log command will show the logs for a specified range of commits. Log is a simple way to review revision notes over a specified range or for the implementation of a specified feature. Shortlog https://git-scm.com/docs/git-mv The shortlog command is a useful command which summarizes the output of the log command. When reviewing the revision messages over a long range, using shortlog will often present the information is a more palatable and easily readable way. Any specific revisions can be reviewed further using the unabridged log messages. Describe https://git-scm.com/docs/git-describe The describe ''command is used to provide an object with an easily readable name based on available references. This command can be useful for large repositories in which convenient labeling allows developers to find objects pertaining to specified topics easier. Patching While the developmental timeline of a project should always remain clean, often branch history can become convoluted from hasty or ill-timed commits. By using patching commands, one is able to modify and restore a pristine history by applying changes, called patches, to branches either at the point in history originally committed or as new commits structured in a more thought-out manner. Apply https://git-scm.com/docs/git-apply The ''apply ''command will apply a patch to specified files or to the index. Patches can be applied to either the files or the index, or it can be applied to both. Cherry-pick https://git-scm.com/docs/git-cherry-pick ''Cherry-pick is used to apply changes specified by specified existing commits. Using this command, previously applied changes can be applied to the HEAD without the need to manually make the changes again. Rebase https://git-scm.com/docs/git-rebase The rebase command is used to reapply commits on top of another branch. Using rebase, the updated branch's history will be the same as if the branch was initially based off of those commits. Revert https://git-scm.com/docs/git-revert The revert command is used to undo one or more existing commits. Using revert new commits will be created with indicate that the existing commits were undone. Debugging Creating projects is not without setbacks, and bugs in a program are an inevitability. Luckily, Git comes with a selection of very useful tools to aid in debugging. With the debugging commands, a developer is able to locate bugs in the timeline, determine who causes the bugs, and search the repository for any relevant information. Bisect https://git-scm.com/docs/git-bisect The bisect command is used to determine which commit introduced a known bug. By specifying a commit that contains the bug, or is "bad," and specifying a commit that does not contain the bug, or is "good," bisect will pick commits between the two endpoint for the user to determine if the bug is present. By continuing this process, the exact commit which introduced the bug can be found much easier than by manually selecting and reviewing commits. Blame https://git-scm.com/docs/git-blame The blame command is used to show the revision and author that last modified each line of a file. By doing so, one can "blame" the author who last modified a line of code which created a bug. Grep https://git-scm.com/docs/git-grep Grep ''is a command familiar to many computer programs, and implementations of the command have existed for many years. ''Grep, which stands for 'G'lobal 'R'egular 'E'xpression 'P'rint, is used to search specified fiels or directories for a matching pattern. For example, one could search an entire repository for any file that contains the work "cat" or the number 7.Category:Setup and Config __FORCETOC__